import { Controller, Get, Post, Delete, Put, Inject, Param, Body } from '@nestjs/common';
import { User } from '../model/entity/user.entity'
import { UserService } from './../service/user.service';
import { Result } from '../util/result.interface'

/**
 * 功能说明：用户控制器
 *
 * @export
 * @class UserController
 */
@Controller('user')
export class UserController {
  //依赖注入
  constructor(@Inject(UserService) private readonly userService: UserService) { }

  /**
   * 功能说明：添加用户信息
   *
   * @param {User} user
   * @return {*}  {Promise<Result>}
   * @memberof UserController
   */
  @Post()
  async addUser(@Body() user: User): Promise<Result> {
    const data = await this.userService.createUser(user);
    if(data) {
      console.log(data);
    }
    return { code: 200, message: '创建成功' };
  }

  /**
   * 功能说明：删除用户
   *
   * @param {number} id
   * @return {*}  {Promise<Result>}
   * @memberof UserController
   */
  @Delete(':id')
  async deleteUser(@Param('id') id: number): Promise<Result> {
    const data = await this.userService.deleteUser(id);
    if(data) {
      console.log(data);
    }
    return { code: 200, message: '删除成功' };
  }

  /**
   * 功能说明：更新用户数据
   *
   * @param {number} id
   * @param {User} user
   * @return {*}  {Promise<Result>}
   * @memberof UserController
   */
  @Put(':id')
  async updateUser(@Param('id') id: number, @Body() user: User): Promise<Result> {
    await this.userService.updateUser(id, user);
    return { code: 200, message: '更新成功' };
  }

  /**
   * 功能说明：查询一个用户信息
   *
   * @param {number} id
   * @return {*}  {Promise<Result>}
   * @memberof UserController
   */
  @Get(':id')
  async queryOne(@Param('id') id: number): Promise<Result> {
    const data = await this.userService.findById(id);
    console.log(data);
    return { code: 200, message: '查询成功', data: data };
  }

  /**
   * 功能说明：查询符合条件的用户数据。
   *
   * @param {User} user
   * @return {*}  {Promise<Result>}
   * @memberof UserController
   */
  @Get()
  async query(@Body() user: User): Promise<any> {
    const users = await this.userService.findByUser(user);
    console.log(users);
    if(users) {
      return { code: 200, message: '查询成功' };
    }
    return { code: 200, message: '查询成功', data: users };;
  }
}
